<?php 
	class RegControl extends CommonControl{
		
		

		/**
		 * 验证码
	 	*/
	public function code(){
		$code = new code();
		$code->show();
	}
	public function register(){
		if (!IS_POST)$this->error('页面不存在');
		$code=trim(adds_html($_POST['code']));
		if(strtoupper($code)!=$_SESSION['code'])$this->error('验证码不对');
		$user=trim(adds_html($_POST['username']));
		if(empty($user))$this->error('用户名不能为空');
		$preg = "/^[a-z][0-9a-z-_]{3,8}[0-9a-z]$/i";
		if(!preg_match($preg, $user))$this->error('用户名不合法');

		$result=M('user')->where('username='."'".$user."'")->all();
		if ($result!=null) {
			$this->error('用户名已存在');
		}
		$password=trim(adds_html($_POST['pwd']));
		$password1=trim(adds_html($_POST['pwded']));
		if ($password==NULL)$this->error('密码不能为空');
		if (strlen($password)<6||strlen($password)>20)$this->error('密码长度不对');
		if($password!==$password1)$this->error('密码不一致');
		$data=array(
            'username'=>$user,
            'passwd'=>md5($password),
            'restime'=>time()
        );
		$bloon=M('user')->add($data);
		if($bloon)$this->success('注册成功',__ROOT__);
	}
		/**
	 * 异步检测用户名
	 */
	public function ajax_username(){
		// echo 'houdunwang';
		if(!IS_AJAX) $this->error('页面不存在');
		$username = "'".$_POST['username']."'";
		if(M('user')->where('username='.$username)->field('uid')->find()){
			echo 0;
		} else {
			echo 1;
		}
	}
	/**
	 * 异步判断验证码
	 */
	public function ajax_code(){
		if(!IS_AJAX) $this->error('页面不存在');
		$code  = strtoupper(adds_html($_POST['verify']));
		if($code != $_SESSION['code']){
			echo 0;
		} else {
			echo 1;
		}
	}
	}
	





 ?>